package com.sakura.mall.product.brand.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.brand.domain.entity.BrandEntity;
import com.sakura.mall.product.brand.domain.request.AddBrandRequest;
import com.sakura.mall.product.brand.domain.request.UpdateBrandRequest;
import com.sakura.mall.product.brand.domain.request.BrandPageRequest;
import com.sakura.mall.product.brand.domain.vo.BrandVo;
import com.sakura.mall.product.brand.mapper.BrandMapper;
import com.sakura.mall.product.brand.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 品牌 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class BrandServiceImpl extends ServiceImpl<BrandMapper, BrandEntity> implements BrandService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddBrandRequest request) {
        // todo 其他校验逻辑
        BrandEntity brand = request.convert(BrandEntity.class);
        this.save(brand);
    }

    @Override
    public Page<BrandVo> queryPage(BrandPageRequest request) {
        IPage<BrandEntity> qp = request.toPage();
        LambdaQueryWrapper<BrandEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<BrandVo> ret = qp.convert(entity -> entity.convert(BrandVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateBrandRequest request) {
        BrandEntity entity = request.convert(BrandEntity.class);
        entity.setBrandId(id);
        this.updateById(entity);
    }

}

